<?php
declare (strict_types=1);

namespace app\index\controller;

use app\common\model\Assets;
use app\common\model\Bank;
use app\common\model\Relation;
use app\common\tool\Result;
use think\exception\ValidateException;
use think\facade\View;
use app\common\model\User as UserModel;

class User extends Base
{

    public function index()
    {
        $orderStatus = [
            ['status' => 1, 'title' => '待付款', 'count' => getOrderCount($this->user->id, 1)],
            ['status' => 2, 'title' => '待发货', 'count' => getOrderCount($this->user->id, 2)],
            ['status' => 3, 'title' => '待收货', 'count' => getOrderCount($this->user->id, 3)],
            ['status' => 4, 'title' => '已完成', 'count' => 0]
        ];
        $fun = [
            ['icon' => 'ic_wodqbao@2x.png', 'title' => '我的钱包', 'url' => url('account/index')->build()],
            ['icon' => 'ic_wodwytix@2x.png', 'title' => '我要提现', 'url' => url('draw/index')->build()],
            ['icon' => 'ic_wodwyaozz@2x.png', 'title' => '我要转账', 'url' => url('transfer/index')->build()],
            ['icon' => 'ic_wodwyaozz@2x.png', 'title' => '我要转换', 'url' => url('exchange/index')->build()],
            ['icon' => 'ic_wodwyczhi@2x.png', 'title' => '我要充值', 'url' => url('remit/index')->build()],
            ['icon' => 'ic_wodtdui@2x.png', 'title' => '我的团队', 'url' => url('user/my_team')->build()],
            ['icon' => 'ic_wodlxkfu@2x.png', 'title' => '联系客服', 'url' => url('user/contact')->build()],
            ['icon' => 'ic_wodyqhyou@2x.png', 'title' => '邀请好友', 'url' => url('user/share')->build()],
            ['icon' => 'ic_wodtsjyi@2x.png', 'title' => '投诉建议', 'url' => url('message/index')->build()]
        ];
        return View::fetch('', [
            'assets'      => Assets::getFields(3),
            'orderStatus' => $orderStatus,
            'fun'         => $fun
        ]);
    }

    public function info()
    {
        return View::fetch();
    }

    public function setting()
    {
        return View::fetch();
    }

    public function contact()
    {
        return View::fetch();
    }

    public function my_team()
    {
        if ($this->request->isAjax()) {
            $page = $this->request->param('page', 1);
            $map = ['uid' => $this->user->id, 'algebra' => 1];
            return json(Relation::getPageList($map, '*', $page, 20, false, ['fresh']));
        }
        $dpn = Relation::where(['uid' => $this->user->id, 'algebra' => 1])->column('new_uid');
        $team = Relation::where('uid', $this->user->id)->count();
        View::assign([
            'dpn'        => count($dpn),
            'dpn_total'  => UserModel::whereIn('id', $dpn)->sum('my_total'),
            'team'       => $team,
            'team_total' => $this->user->team_total
        ]);
        return View::fetch();
    }

    public function methodList()
    {
        return View::fetch();
    }

    public function method($type)
    {
        $mode = ['alipay' => '支付宝', 'wechat' => '微信', 'bank' => '银行卡'];
        View::assign([
            'type'      => $type,
            'title'     => $mode[$type],
            'bank_list' => Bank::where(['status' => 1])->field('id,title')->select()->each(function (&$d) {
                if ($this->user->bank_name == $d['title']) {
                    $d->check = true;
                }
            })
        ]);
        return View::fetch();
    }

    public function method_save()
    {
        try {
            $type = $this->request->post('type');
            $param = $this->request->post(['name', 'image', 'bank_name', 'bank_open', 'bank_account_name', 'bank_number']);

            $v = $type == 'bank' ? 'MethodBank' : 'MethodImg';
            try {
                $this->validate($param, $v);
            } catch (ValidateException $e) {
                return json(['code' => -1, 'msg' => $e->getError()]);
            }
            switch ($type) {
                case "bank":
                    break;
                default:
                    $param[$type . '_name'] = $param['name'];
                    $param[$type . '_image'] = $param['image'];
                    unset($param['name']);
                    unset($param['image']);
            }

            UserModel::update($param, ['id' => $this->user->id]);

            return json(['code' => 1, 'msg' => '已保存']);

        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

    public function edit()
    {
        try {
            if ($this->request->isPost()) {
                $param = $this->request->post(['nickname', 'photo', 'mobile']);
                UserModel::update($param, ['id' => $this->user->id]);
                return Result::success('已修改');
            }
            return View::fetch('', [
                'type'  => $this->request->get('type'),
                'title' => ['nickname' => '昵称', 'mobile' => '手机号']
            ]);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }

    }

    public function logout()
    {
        $token = md5(time() . $this->user['id'] . $this->request->ip());

        UserModel::update([
            'token' => $token
        ], ['id' => $this->user['id']]);
        cookie('token', null);

        return Result::fastReturn(['code' => 1, 'msg' => '已退出登录', 'url' => url('login/index')->build()]);
    }

    public function share()
    {
        $url = "http://" . $_SERVER['SERVER_NAME'] . "/index/register/index.html?code=" . $this->user->code;
        View::assign('url', $url);
        return View::fetch();
    }

    public function release()
    {
        if ($this->request->isAjax()) {
            return Result::fastReturn(UserRelease::getPageList(['uid' => $this->user->id], '*', $this->getPage(), 20, false));
        }
        return View::fetch();
    }

}

